import common from '@ohos.app.ability.common';
import window from '@ohos.window';
import bundleManager from '@ohos.bundle.bundleManager';
import resourceManager from '@ohos.resourceManager';
import HashMap from '@ohos.util.HashMap';
import { JsQtModule } from './JsQtModule'
import deviceInfo from '@ohos.deviceInfo';
import QtJsObjectLoader from './JsObjectLoader';

interface ApplicationDirs {
  tempDir: string,
  filesDir: string,
  cacheDir: string,
  databaseDir: string,
  bundleCodeDir: string,
  preferencesDir: string,
  distributedFilesDir: string,
  qmlDir: string
}

class JsDataStore {
  private windowName: string;
  private context: common.UIAbilityContext = null;
  private windowStage: window.WindowStage;
  private elementName: bundleManager.ElementName = null;
  private jsModules: JsQtModule[] = [];
  private applicationDirs: ApplicationDirs;
  private qtMajorVersion: number = 0;
  private qtNativeModules: HashMap<string, any> = new HashMap();
  private jsObjectLoader: QtJsObjectLoader = new QtJsObjectLoader;

  constructor() {
  }

  setContext(context: common.UIAbilityContext) {
    this.context = context;
    let appContext = this.context.getApplicationContext();
    let dirs : ApplicationDirs = {
      tempDir: appContext.tempDir,
      filesDir: appContext.filesDir,
      cacheDir: appContext.cacheDir,
      databaseDir: appContext.databaseDir,
      bundleCodeDir: appContext.bundleCodeDir,
      preferencesDir: appContext.preferencesDir,
      distributedFilesDir: appContext.distributedFilesDir,
      qmlDir: appContext.cacheDir + "/Qt/qml"
    };
    this.applicationDirs = dirs;
  }

  getContext(): common.UIAbilityContext {
    return this.context;
  }

  getApplicationDirs() : ApplicationDirs {
    return this.applicationDirs;
  }

  setElementName(en: bundleManager.ElementName) {
    this.elementName = en;
  }

  getElementName(): bundleManager.ElementName {
    return this.elementName;
  }


  setWindowStage(windowStage: window.WindowStage) {
    this.windowStage = windowStage;
  }

  getWindowStage(): window.WindowStage {
    return this.windowStage;
  }

  getResourceManager() : resourceManager.ResourceManager {
    return this.context.resourceManager;
  }

  addJsModule(module: JsQtModule) {
    this.jsModules.push(module);
  }

  getJsModules(): JsQtModule[] {
    return this.jsModules;
  }

  getJsObjectLoader(): QtJsObjectLoader {
    return this.jsObjectLoader;
  }

  getQtMajorVersion(): number {
    return this.qtMajorVersion;
  }

  setQtMajorVersion(version: number) {
    this.qtMajorVersion = version;
  }

  addQtNativeModule(name: string, module: any) {
    this.qtNativeModules.set(name, module);
  }

  getQtNativeModule(name: string): any {
    return this.qtNativeModules.get(name);
  }

  deviceType(): string {
    return 'default';
    // return deviceInfo.deviceType;
  }
}

export default new JsDataStore;